﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace Mengsk.ProxyDelivery.Converters
{
    public class WarehouseConverter : IValueConverter
    {
        private WarehouseService warehouseService = ServiceContainer.GetService<WarehouseService>();
        private static readonly Dictionary<int, string> warehouses = new Dictionary<int, string>();

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || value.GetType() != typeof(int))
            {
                return value;
            }

            int id = (int)value;
            if (warehouses.ContainsKey(id))
            {
                return warehouses[id];
            }

            lock (warehouses)
            {
                if (warehouses.ContainsKey(id) == false)
                {
                    var item = this.warehouseService.GetById(id);
                    if (item != null)
                    {
                        warehouses[id] = item.Name;
                        return item.Name;
                    }
                    return "仓库不存在";
                }
                return warehouses[id];
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
